package com.xrtn.controller;

import com.xrtn.bean.SysUserInfo;
import com.xrtn.bean.TSubclinicInfo;
import com.xrtn.service.ICommonService;
import com.xrtn.utils.CodeEnum;
import com.xrtn.utils.ObjUtils;
import com.xrtn.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tu_pei on 2018/3/5.
 */
@Api("门店相关接口")
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/subclinic")
public class SubclinicController {

    @Autowired
    private ICommonService commonService;

    /**
     * 进入门店管理页面
     * @return
     */
    @RequestMapping(value = "/gotoStoreSetting",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoStoreSetting(ModelAndView modelAndView){
        modelAndView.setViewName("storeSetting");
        return modelAndView;
    }
    /**
     * 进入门店详情页面
     * @return
     */
    @RequestMapping(value = "/gotoStoreDetail",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoStoreDetail(ModelAndView modelAndView,Integer subclinicId){
        modelAndView.setViewName("storeDetail");
        return modelAndView;
    }
    /**
     * 查询门店列表
     * @return
     */
    @ApiOperation(value = "查询门店列表",notes="查询门店列表")
    @RequestMapping(value = "/selectSubclinicList",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectSubclinicList(Integer subclinicId){

        ResponseData data = new ResponseData();

        Map<String,Object> params = new HashMap<>();
        params.put("subclinicId", subclinicId);
        List<TSubclinicInfo> subclinicList = commonService.selectSubclinicList(params);

        data.setResults(subclinicList);

        // 返回数据
        return data;
    }

    /**
     * 新增门店
     * @return
     */
    @ApiOperation(value = "新增门店",notes="新增门店")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="subclinicName",dataType="string",required=false,value="门店名称",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicLogo",dataType="string",required=false,value="门店LOGO",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicPhone",dataType="string",required=false,value="门店联系人电话",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicStartTime",dataType="string",required=false,value="营业开始时间",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicEndTime",dataType="string",required=false,value="营业结束时间",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicAddress",dataType="string",required=false,value="门店地址",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicLatLon",dataType="string",required=false,value="门店所在经纬度",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicDescInfo",dataType="string",required=false,value="分店介绍",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicDescImg",dataType="string",required=false,value="分店介绍图片",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicHonorInfo",dataType="string",required=false,value="分店荣誉",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicHonorImg",dataType="string",required=false,value="分店荣誉图片",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicEnvironmentInfo",dataType="string",required=false,value="分店环境",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicEnvironmentImg",dataType="string",required=false,value="分店环境图片",defaultValue = "1"),
    })
    @RequestMapping(value = "/saveSubclinic",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData saveSubclinic(TSubclinicInfo subclinicInfo){

        ResponseData data = new ResponseData();

        commonService.saveSubclinic(subclinicInfo);

        // 返回数据
        return data;
    }

    /**
     * 查询门店信息
     * @return
     */
    @ApiOperation(value = "查询门店信息",notes="查询门店信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="subclinicId",dataType="string",required=false,value="门店id",defaultValue = "1"),
    })
    @RequestMapping(value = "/selectSubclinicInfo",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData saveSubclinic(Integer subclinicId){

        ResponseData data = new ResponseData();

        data.setResults(commonService.selectSubclinicInfoByKey(subclinicId));

        // 返回数据
        return data;
    }

    /**
     * 查询门店列表
     * @return
     */
    @ApiOperation(value = "修改门店信息",notes="修改门店信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="subclinicId",dataType="string",required=false,value="门店Id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicName",dataType="string",required=false,value="门店名称",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicLogo",dataType="string",required=false,value="门店LOGO",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicPhone",dataType="string",required=false,value="门店联系人电话",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicStartTime",dataType="string",required=false,value="营业开始时间",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicEndTime",dataType="string",required=false,value="营业结束时间",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicAddress",dataType="string",required=false,value="门店地址",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicLatLon",dataType="string",required=false,value="门店所在经纬度",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicDesc",dataType="string",required=false,value="备注",defaultValue = "1"),
    })
    @RequestMapping(value = "/updateSubclinic",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData updateSubclinic(TSubclinicInfo subclinicInfo){

        ResponseData data = new ResponseData();

        commonService.updateSubclinic(subclinicInfo);

        // 返回数据
        return data;
    }


    @ApiOperation(value = "设置店长", notes = "设置店长")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = true, value = "门店id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "ownerId", dataType = "int", required = true, value = "店长id", defaultValue = "1"),
    })
    @RequestMapping(value = "/updateOwner", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData updateOwner(int subclinicId, int ownerId) {
        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{subclinicId, ownerId});
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        } else {
            int owner = commonService.updateOwner(subclinicId, ownerId);
            data.setResults(owner);
            // 返回数据
            return data;
        }
    }

}
